package de.eventdroid;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.eventdroid.helper_classes.JSONHandler;
import de.eventdroid.helper_classes.RESTHandler;
import de.eventdroid.helper_classes.TextHandler;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EventResultScreen extends Activity {
	
	/**
	 * Declaration of needed variables
	 */;
	private String resultString = null;
	private ArrayList<String> aktFoundObjectsForListViewArray = new ArrayList<String>();
	private ArrayList<String> aktFoundObjectIDs = new ArrayList<String>();
	private ListAdapter listenAdapter = null;
	private ArrayAdapter<String> eventArrayAdapter = null;
	private ListView aktListView;
	private Intent showSpecEventData;
	public Activity local_activity;
	/* Loading dialog */
	ProgressDialog dialog;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdroid_event_result_screen);
        
        /* local activity */
        local_activity = this;
        
        this.fetchEventResultsForListView(); 
        
        aktListView = (ListView)findViewById(R.id.listview_event_result_list_view);
    	showSpecEventData = new Intent(this, EventSpecialResultScreen.class);
    	
    	aktListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View aktView, int clickedPosition,
					long arg3) {
				
				/* show loading dialog */
				dialog = ProgressDialog.show(local_activity, "", "Bitte warten, Detaildaten werden geladen....", true);
				
				aktListView.getItemAtPosition(clickedPosition);						
				showSpecEventData.putExtra("event_data", aktFoundObjectIDs.get(clickedPosition));
				dialog.cancel();
				startActivity(showSpecEventData);
				
			}
		});
        
    }
    
    /**
     * Worker Class for fetching the Event Results for ListView 
     */
    private void fetchEventResultsForListView() {
    	
    	/* Fetch ressource data from Server */
		resultString = RESTHandler.getData("eventHandler");
    	
    	/* Parse the JSON String into  */
    	aktFoundObjectsForListViewArray = JSONHandler.parseJSONDataForListView(resultString, "event", true);
    	
    	/* Extract Location IDs from content */
    	aktFoundObjectIDs = TextHandler.extractIDFromResultArray(aktFoundObjectsForListViewArray);
    	
    	/* Cleanup List Location from IDs */
    	aktFoundObjectsForListViewArray = TextHandler.cleanUpArrayFromIds(aktFoundObjectsForListViewArray);
    	
	    /* Allocate ListAdapter for filling the ListView */
	    aktListView = (ListView) findViewById(R.id.listview_event_result_list_view);
        eventArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, aktFoundObjectsForListViewArray);
        eventArrayAdapter.notifyDataSetChanged();
	    listenAdapter = eventArrayAdapter;
	    aktListView.setAdapter(listenAdapter);
	    
	    /* Number of found Locations in Toast */
	    Toast.makeText(getBaseContext(), "Es wurden "+aktFoundObjectsForListViewArray.size()+" Events gefunden." ,Toast.LENGTH_LONG).show();
	
    }
    
    /**
     * Creation Class for the menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
    		case R.id.menu_startscreen:     
    			startActivity(new Intent(this, StartScreen.class));           	
    			break;
    	}
    	return true;
    }

}